home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / Voyeur 1.1.1 / Voyeur ƒ / v code ƒ / v meat.c < prev    next >
Text File  |  1994-02-26  |  9KB  |  328 lines

  1. /**********************************************************************\
  2.  
  3. File:        v meat.c
  4.  
  5. Purpose:    This module handles the options under the Options menu:
  6.             going forward one block, going backward one block, etc.
  7.             These procedures actually change the internal variables
  8.             that tell us what block we're on and then update the
  9.             screen accordingly.  Also, changing the fork EOF is
  10.             handled here.
  11.  
  12.  
  13. Voyeur -- a no-frills file viewer
  14. Copyright ©1993-4, Mark Pilgrim
  15.  
  16. This program is free software; you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation; either version 2 of the License, or
  19. (at your option) any later version.
  20.  
  21. This program is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. GNU General Public License for more details.
  25.  
  26. You should have received a copy of the GNU General Public License
  27. along with this program in a file named "GNU General Public License".
  28. If not, write to the Free Software Foundation, 675 Mass Ave,
  29. Cambridge, MA 02139, USA.
  30.  
  31. \**********************************************************************/
  32.  
  33. #include "v meat.h"
  34. #include "program globals.h"
  35. #include "v hex.h"
  36. #include "v window maintenance.h"
  37. #include "v file management.h"
  38. #include "v error.h"
  39. #include "v.h"
  40. #include "msg graphics.h"
  41. #include "msg dialogs.h"
  42.  
  43. void ChangeEOF(void)
  44. {
  45.     DialogPtr        theDlog;
  46.     int                itemSelected;
  47.     Handle            itemH;
  48.     short            item,itemType;
  49.     Rect            box;
  50.     Str255            tempStr;
  51.     unsigned long    temptotal;
  52.     OSErr            isHuman;
  53.     
  54.     PositionDialog('DLOG', eofDialog);
  55.     theDlog = GetNewDialog(eofDialog, 0L, (WindowPtr)-1L);
  56.  
  57.     GetDItem(theDlog, 5, &itemType, &itemH, &box);
  58.     InsetRect(&box, -4, -4);
  59.     SetDItem(theDlog, 5, itemType, (Handle)OutlineDefaultButton, &box);
  60.     
  61.     GetDItem(theDlog,4,&itemType,&itemH,&box);
  62.     LongToHexString(gForkLength[gWhichFile][gWhichFork[gWhichFile]], tempStr);
  63.     SetIText(itemH, tempStr);
  64.     SelIText(theDlog, 4, 0, 32767);
  65.     SetWTitle((WindowPtr)theDlog, "\pSet EOF");
  66.     
  67.     ShowWindow(theDlog);
  68.     
  69.     itemSelected=0;
  70.     while ((itemSelected!=1) && (itemSelected!=2))
  71.     {
  72.         ModalDialog((ProcPtr)HexOFilter, &itemSelected);
  73.     }
  74.     
  75.     if (itemSelected==1)
  76.     {
  77.         GetDItem(theDlog,4,&itemType,&itemH,&box);
  78.         GetIText(itemH, tempStr);
  79.         if (tempStr[0]==0x00)
  80.             itemSelected=2;
  81.     }
  82.  
  83.     HideWindow(theDlog);
  84.     DisposeDialog(theDlog);
  85.  
  86.     if (itemSelected==1)
  87.     {
  88.         if (!ValidHex(tempStr))
  89.             HandleError(invalidHexErr);
  90.         else if (tempStr[0]>8)
  91.             HandleError(offsetTooLargeErr);
  92.         else
  93.         {
  94.             temptotal=HexStringToLong(tempStr);
  95.             isHuman=SetEOF(gTheRefNum[gWhichFile], temptotal);
  96.             if (isHuman!=noErr)
  97.             {
  98.                 SetEOF(gTheRefNum[gWhichFile], gForkLength[gWhichFile][gWhichFork[gWhichFile]]);
  99.                 HandleError(diskFullErr);
  100.             }
  101.             else
  102.             {
  103.                 gForkLength[gWhichFile][gWhichFork[gWhichFile]]=temptotal;
  104.                 if (gTheOffset[gWhichFile][gWhichFork[gWhichFile]]+gBufferOffset[gWhichFile]>=temptotal)
  105.                 {
  106.                     if (temptotal%256)
  107.                     {
  108.                         gTheOffset[gWhichFile][gWhichFork[gWhichFile]]=(temptotal/256)*256;
  109.                         gBufferOffset[gWhichFile]=temptotal-gTheOffset[gWhichFile][gWhichFork[gWhichFile]];
  110.                     }
  111.                     else
  112.                     {
  113.                         gTheOffset[gWhichFile][gWhichFork[gWhichFile]]=(temptotal==0L) ? 0 : temptotal-256;
  114.                         gBufferOffset[gWhichFile]=0;
  115.                     }
  116.                 }
  117.                 GetBuffer(gTheRefNum[gWhichFile], gTheOffset[gWhichFile][gWhichFork[gWhichFile]], gTheBuffer[gWhichFile]);
  118.                 UpdateProgramWindow(gWhichFile, TRUE);
  119.             }
  120.         }
  121.     }
  122. }
  123.  
  124. void DoDataFork(void)
  125. {
  126.     int            resultCode;
  127.     
  128.     if (gWhichFork[gWhichFile]==1)
  129.     {
  130.         CloseTheFile(gTheRefNum[gWhichFile]);
  131.         gWhichFork[gWhichFile]=0;
  132.         gBufferOffset[gWhichFile]=0;
  133.         resultCode=OpenTheDataFork(&gTheFS[gWhichFile], &gTheRefNum[gWhichFile]);
  134.         
  135.         if (resultCode==allsWell)
  136.             resultCode=GetBuffer(gTheRefNum[gWhichFile], gTheOffset[gWhichFile][gWhichFork[gWhichFile]], gTheBuffer[gWhichFile]);
  137.  
  138.         if (resultCode!=allsWell)
  139.         {
  140.             HandleError(resultCode);
  141.             CloseProgramWindow(gWhichFile);
  142.         }
  143.         else
  144.             UpdateProgramWindow(gWhichFile, TRUE);
  145.     }
  146. }
  147.  
  148. void DoResourceFork(void)
  149. {
  150.     int            resultCode;
  151.     
  152.     if (gWhichFork[gWhichFile]==0)
  153.     {
  154.         CloseTheFile(gTheRefNum[gWhichFile]);
  155.         gWhichFork[gWhichFile]=1;
  156.         gBufferOffset[gWhichFile]=0;
  157.         resultCode=OpenTheResourceFork(&gTheFS[gWhichFile], &gTheRefNum[gWhichFile]);
  158.  
  159.         if (resultCode==allsWell)
  160.             resultCode=GetBuffer(gTheRefNum[gWhichFile], gTheOffset[gWhichFile][gWhichFork[gWhichFile]], gTheBuffer[gWhichFile]);
  161.         
  162.         if (resultCode!=allsWell)
  163.         {
  164.             HandleError(resultCode);
  165.             CloseProgramWindow(gWhichFile);
  166.         }
  167.         else
  168.             UpdateProgramWindow(gWhichFile, TRUE);
  169.     }
  170. }
  171.  
  172. void GoForward(void)
  173. {
  174.     int            resultCode;
  175.     
  176.     if (gTheOffset[gWhichFile][gWhichFork[gWhichFile]]+256L<gForkLength[gWhichFile][gWhichFork[gWhichFile]])
  177.     {
  178.         gTheOffset[gWhichFile][gWhichFork[gWhichFile]]+=256L;
  179.         gBufferOffset[gWhichFile]=0;
  180.         resultCode=GetBuffer(gTheRefNum[gWhichFile], gTheOffset[gWhichFile][gWhichFork[gWhichFile]], gTheBuffer[gWhichFile]);
  181.         if (resultCode!=allsWell)
  182.         {
  183.             HandleError(resultCode);
  184.             CloseProgramWindow(gWhichFile);
  185.         }
  186.         
  187.         UpdateProgramWindow(gWhichFile, TRUE);
  188.     }
  189.     else SysBeep(7);
  190. }
  191.  
  192. void GoBack(void)
  193. {
  194.     int            resultCode;
  195.     
  196.     if (gTheOffset[gWhichFile][gWhichFork[gWhichFile]]>=256L)
  197.     {
  198.         gTheOffset[gWhichFile][gWhichFork[gWhichFile]]-=256L;
  199.         gBufferOffset[gWhichFile]=0;
  200.         resultCode=GetBuffer(gTheRefNum[gWhichFile], gTheOffset[gWhichFile][gWhichFork[gWhichFile]], gTheBuffer[gWhichFile]);
  201.         if (resultCode!=allsWell)
  202.         {
  203.             HandleError(resultCode);
  204.             CloseProgramWindow(gWhichFile);
  205.         }
  206.         else
  207.             UpdateProgramWindow(gWhichFile, TRUE);
  208.     }
  209.     else SysBeep(7);
  210. }
  211.  
  212. void GoBeginning(void)
  213. {
  214.     int            resultCode;
  215.     
  216.     if (gTheOffset[gWhichFile][gWhichFork[gWhichFile]]!=0)
  217.     {
  218.         gTheOffset[gWhichFile][gWhichFork[gWhichFile]]=0L;
  219.         gBufferOffset[gWhichFile]=0;
  220.         resultCode=GetBuffer(gTheRefNum[gWhichFile], gTheOffset[gWhichFile][gWhichFork[gWhichFile]], gTheBuffer[gWhichFile]);
  221.         if (resultCode!=allsWell)
  222.         {
  223.             HandleError(resultCode);
  224.             CloseProgramWindow(gWhichFile);
  225.         }
  226.         else
  227.             UpdateProgramWindow(gWhichFile, TRUE);
  228.     }
  229. }
  230.  
  231. void GoEnd(void)
  232. {
  233.     int                resultCode;
  234.     unsigned long    temp;
  235.     
  236.     if (gForkLength[gWhichFile][gWhichFork[gWhichFile]]%256)
  237.         temp=(gForkLength[gWhichFile][gWhichFork[gWhichFile]]/256)*256;
  238.     else
  239.         temp=gForkLength[gWhichFile][gWhichFork[gWhichFile]]-256;
  240.     
  241.     if (gTheOffset[gWhichFile][gWhichFork[gWhichFile]]!=temp)
  242.     {
  243.         gTheOffset[gWhichFile][gWhichFork[gWhichFile]]=temp;
  244.         gBufferOffset[gWhichFile]=0;
  245.         resultCode=GetBuffer(gTheRefNum[gWhichFile], gTheOffset[gWhichFile][gWhichFork[gWhichFile]], gTheBuffer[gWhichFile]);
  246.         if (resultCode!=allsWell)
  247.         {
  248.             HandleError(resultCode);
  249.             CloseProgramWindow(gWhichFile);
  250.         }
  251.         else
  252.             UpdateProgramWindow(gWhichFile, TRUE);
  253.     }
  254. }
  255.  
  256. void GoOffset(void)
  257. {
  258.     DialogPtr        theDlog;
  259.     int                itemSelected;
  260.     Handle            itemH;
  261.     short            item,itemType;
  262.     Rect            box;
  263.     Str255            tempStr;
  264.     int                i;
  265.     unsigned long    tempoff;
  266.             
  267.     PositionDialog('DLOG', offsetDialog);
  268.     theDlog = GetNewDialog(offsetDialog, 0L, (WindowPtr)-1L);
  269.  
  270.     GetDItem(theDlog, 5, &itemType, &itemH, &box);
  271.     InsetRect(&box, -4, -4);
  272.     SetDItem(theDlog, 5, itemType, (Handle)OutlineDefaultButton, &box);
  273.     
  274.     LongToHexString(gTheOffset[gWhichFile][gWhichFork[gWhichFile]], tempStr);
  275.     GetDItem(theDlog,4,&itemType,&itemH,&box);
  276.     SetIText(itemH, tempStr);
  277.     SelIText(theDlog, 4, 0, 32767);
  278.     SetWTitle((WindowPtr)theDlog, "\pGo to offset");
  279.     
  280.     ShowWindow(theDlog);
  281.     
  282.     itemSelected=0;
  283.     while ((itemSelected!=1) && (itemSelected!=2))
  284.     {
  285.         ModalDialog((ProcPtr)HexOFilter, &itemSelected);
  286.     }
  287.     
  288.     if (itemSelected==1)
  289.     {
  290.         GetDItem(theDlog,4,&itemType,&itemH,&box);
  291.         GetIText(itemH, tempStr);
  292.         if (tempStr[0]==0x00)
  293.             itemSelected=2;
  294.     }
  295.  
  296.     HideWindow(theDlog);
  297.     DisposeDialog(theDlog);
  298.  
  299.     if (itemSelected==1)
  300.     {
  301.         if (!ValidHex(tempStr))
  302.             HandleError(invalidHexErr);
  303.         else if (tempStr[0]>8)
  304.             HandleError(offsetTooLargeErr);
  305.         else
  306.         {
  307.             tempoff=HexStringToLong(tempStr);
  308.             if (tempoff>=gForkLength[gWhichFile][gWhichFork[gWhichFile]])
  309.                 HandleError(offsetTooLargeErr);
  310.             else
  311.             {
  312.                 if (tempoff%256)
  313.                 {
  314.                     gTheOffset[gWhichFile][gWhichFork[gWhichFile]]=(tempoff/256)*256;
  315.                     gBufferOffset[gWhichFile]=tempoff-gTheOffset[gWhichFile][gWhichFork[gWhichFile]];
  316.                 }
  317.                 else
  318.                 {
  319.                     gTheOffset[gWhichFile][gWhichFork[gWhichFile]]=tempoff;
  320.                     gBufferOffset[gWhichFile]=0;
  321.                 }
  322.                 GetBuffer(gTheRefNum[gWhichFile], gTheOffset[gWhichFile][gWhichFork[gWhichFile]], gTheBuffer[gWhichFile]);
  323.                 UpdateProgramWindow(gWhichFile, TRUE);
  324.             }
  325.         }
  326.     }
  327. }
  328.